﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;

namespace App.Controllers
{
    public class ServicesItemController : BaseController
    {
        //
        // GET: /ServicesItem2/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(string id)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;        
            
            List<ServicesItemNode> tree = new List<ServicesItemNode>();
            if (string.IsNullOrEmpty(id))
            {
                var l1 = from f in dbe.tb_ServicesItem
                         where f.ParentServiceID == f.ItemNO || f.ParentServiceID == null
                         select new ServicesItemNode
                         {
                             ItemNO = f.ItemNO,
                             ItemName = f.ItemName,
                             ItemLevel = f.ItemLevel,
                             Unit = f.Unit,
                             Price = f.Price,
                             Required = f.Required,
                             ServiceContent = f.ServiceContent,
                             ParentServiceID = f.ParentServiceID
                         };
                tree.AddRange(l1);
                foreach (var p in tree)
                {
                    var s = from f in dbe.tb_ServicesItem
                            where f.ParentServiceID == p.ItemNO && f.ParentServiceID != f.ItemNO
                            select new ServicesItemNode
                            {
                                ItemNO = f.ItemNO,
                                ItemName = f.ItemName,
                                ItemLevel = f.ItemLevel,
                                Unit = f.Unit,
                                Price = f.Price,
                                Required = f.Required,
                                ServiceContent = f.ServiceContent,
                                ParentServiceID = f.ParentServiceID,
                            };
                    p.children = new List<ServicesItemNode>();
                    p.children.AddRange(s);
                }
            }
            else
            {
                var l1 = from f in dbe.tb_ServicesItem
                         where f.ParentServiceID == id && f.ParentServiceID != f.ItemNO
                         select new ServicesItemNode
                         {
                             ItemNO = f.ItemNO,
                             ItemName = f.ItemName,
                             ItemLevel = f.ItemLevel,
                             Unit = f.Unit,
                             Price = f.Price,
                             Required = f.Required,
                             ServiceContent = f.ServiceContent,
                             ParentServiceID = f.ParentServiceID
                         };
                tree.AddRange(l1);
            }
            return Json(tree, JsonRequestBehavior.AllowGet);
        }
               
        //
        // POST: /ServicesItem2/Create

        [HttpPost]
        public ActionResult Create(tb_ServicesItem vmodel)
        {
            try
            {
                string strRequired = Request.Form["Required"];
                bool required = !string.IsNullOrEmpty(strRequired);
                var o = new tb_ServicesItem
                {
                    ItemNO = vmodel.ItemNO,
                    ItemName = vmodel.ItemName,
                    Unit = vmodel.Unit,
                    Price = vmodel.Price,
                    Required = required,
                    ParentServiceID = vmodel.ParentServiceID,
                    ServiceContent = vmodel.ServiceContent
                };
                dbe.tb_ServicesItem.AddObject(o);
                dbe.SaveChanges();
                return Json(new { success = true},JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false,
                                 msg = "添加失败"
                                }, 
                                JsonRequestBehavior.AllowGet);
            }
        }
                
        //
        // POST: /ServicesItem2/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, tb_ServicesItem vmodel)
        {
            try
            {
                string strRequired = Request.Form["Required"];
                bool required = !string.IsNullOrEmpty(strRequired);

                var si = dbe.tb_ServicesItem.FirstOrDefault(m => m.ItemNO == id);
                si.ItemName = vmodel.ItemName;
                si.Unit = vmodel.Unit;
                si.Price = vmodel.Price;
                si.ServiceContent = vmodel.ServiceContent;
                si.Required = required;
                dbe.SaveChanges();
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new
                {
                    success = false,
                    msg = "修改失败"
                }, JsonRequestBehavior.AllowGet);
            }
        }
        
        //
        // POST: /ServicesItem2/Delete/5

        [HttpPost]
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList)
                {
                    var obj = dbe.tb_ServicesItem.FirstOrDefault(m => m.ItemNO == id);
                    dbe.tb_ServicesItem.DeleteObject(obj);
                }

                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "删除出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
